package com.cxg.fairy.ec.main.index;

import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cxg.fairy.ec.main.index.ad.ADGoodsListDelegate;
import com.cxg.fairy.ui.R;
import com.cxg.fairy.ui.banner.BannerCreator;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.ItemType;
import com.cxg.fairy.ui.recycler.MultiSnapHorizontalAdapter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.cxg.fairy.ui.widget.CountDownTimer.MainDownTimerView;
import com.cxg.fairy.ui.widget.CountDownTimer.OnCountDownTimerListener;
import com.takusemba.multisnaprecyclerview.MultiSnapRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CFairy
 */

public class MultipleRecyclerAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity, MultipleViewHolder>
        implements BaseQuickAdapter.SpanSizeLookup, OnItemClickListener {

    private boolean mIsInitBanner = false;

    private boolean mIsRushItem = false;

    private IndexDelegate DELEGATE = null;

    //设置图片加载策略
    private static final RequestOptions RECYCLER_OPTIONS = new RequestOptions().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();

    protected MultipleRecyclerAdapter(List<MultipleItemEntity> data) {
        super(data);
        init();
    }

    public static MultipleRecyclerAdapter create(List<MultipleItemEntity> data) {
        return new MultipleRecyclerAdapter(data);
    }

    public static MultipleRecyclerAdapter create(DataConverter converter) {
        return new MultipleRecyclerAdapter(converter.convert());
    }

    private void init() {
        //设置不同的item布局
        addItemType(ItemType.TEXT, R.layout.item_multiple_text);
        addItemType(ItemType.IMAGE, R.layout.item_multiple_image);
        addItemType(ItemType.TEXT_IMAGE, R.layout.item_multiple_image_text);
        addItemType(ItemType.BANNER, R.layout.item_multiple_banner);
        addItemType(ItemType.ICON_IMAGE, R.layout.item_multiple_icon_nav);
        //横向RecyclerView滑动
        addItemType(ItemType.MULTI_SNAP_RECYCLERVIEW,R.layout.item_multisnap_recyclerview);
        //第一张单AD
        addItemType(ItemType.HEADER_MAIN_IMG,R.layout.item_header_main_image);
        //促销商品列表
        addItemType(ItemType.INDEX_GOODS,R.layout.item_multiple_index_goods);

        //固定高度单图两张组合 = 单图一张AD
        addItemType(ItemType.INDEX_IMGS,R.layout.item_multiple_images);
        //普通父子Header
        addItemType(ItemType.INDEX_TITLES,R.layout.item_multiple_titles);

        //设置宽度监听
        setSpanSizeLookup(this);
        openLoadAnimation();
        //多次执行动画
        isFirstOnly(false);
    }

    @Override
    protected MultipleViewHolder createBaseViewHolder(View view) {
        return MultipleViewHolder.create(view);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, MultipleItemEntity entity) {
        final String text;
        final String imageUrl;
        final String headerImgUrl;
        final String iconUrl;
        final String indexImgIdA;
        final String indexImgIdB;

        final int indexImgIDA;
        final int indexImgIDB;

        final String mainTitle;
        final String subTitle;

        final ArrayList<String> bannerImages;
        final ArrayList<JSONObject> mSnapItemList;

        final String goodsTag;
        final String goodsPrice;
        final String goodsUnit;
        final String goodsName;

        final int milliseconds;
        MainDownTimerView mMainDownTimerView;

        switch (holder.getItemViewType()) {
            case ItemType.TEXT:
                text = entity.getField(MultipleFields.TEXT);
                holder.setText(R.id.text_single, text);
                break;
            case ItemType.INDEX_TITLES:
                mainTitle = entity.getField(MultipleFields.MAIN_TITLE);
                subTitle = entity.getField(MultipleFields.SUB_TITLE);
                holder.setText(R.id.index_main_title, mainTitle);
                holder.setText(R.id.index_main_subtitle, subTitle);
                break;
            case ItemType.IMAGE:
                imageUrl = entity.getField(MultipleFields.IMAGE_URL);
                Glide.with(mContext).load(imageUrl).apply(RECYCLER_OPTIONS).into((AppCompatImageView) holder.getView(R.id.img_single));
                break;
            case ItemType.INDEX_IMGS:
                indexImgIdA = entity.getField(MultipleFields.INDEX_IMG_A);
                indexImgIdB = entity.getField(MultipleFields.INDEX_IMG_B);
                indexImgIDA = entity.getField(MultipleFields.INDEX_IMG_A_ID);
                indexImgIDB = entity.getField(MultipleFields.INDEX_IMG_B_ID);
                AppCompatImageView imgViewA = holder.getView(R.id.img_index_sa);
                AppCompatImageView imgViewB = holder.getView(R.id.img_index_sb);
                Glide.with(mContext).load(indexImgIdA).apply(RECYCLER_OPTIONS).into(imgViewA);
                Glide.with(mContext).load(indexImgIdB).apply(RECYCLER_OPTIONS).into(imgViewB);
                imgViewA.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DELEGATE.getParentDelegate().getSupportDelegate().start(ADGoodsListDelegate.create(indexImgIDA));
                    }
                });
                imgViewB.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DELEGATE.getParentDelegate().getSupportDelegate().start(ADGoodsListDelegate.create(indexImgIDB));
                    }
                });
                break;
            case ItemType.TEXT_IMAGE:
                text = entity.getField(MultipleFields.TEXT);
                imageUrl = entity.getField(MultipleFields.IMAGE_URL);
                Glide.with(mContext).load(imageUrl).apply(RECYCLER_OPTIONS).into((AppCompatImageView) holder.getView(R.id.img_multiple));
                holder.setText(R.id.tv_multiple, text);
                break;
            case ItemType.ICON_IMAGE:
                text = entity.getField(MultipleFields.TEXT);
                iconUrl = entity.getField(MultipleFields.ICON_URL);
                Glide.with(mContext).load(iconUrl).apply(RECYCLER_OPTIONS).into((AppCompatImageView) holder.getView(R.id.img_multiple_icon));
                holder.setText(R.id.tv_multiple_text, text);
                break;
            case ItemType.BANNER:
                if (!mIsInitBanner) {
                    bannerImages = entity.getField(MultipleFields.BANNERS);
                    final ConvenientBanner<String> convenientBanner = holder.getView(R.id.banner_recycler_item);
                    BannerCreator.setDefault(convenientBanner, bannerImages, this);
                    mIsInitBanner = true;
                }
                break;
            case ItemType.MULTI_SNAP_RECYCLERVIEW:
                if (!mIsRushItem) {
                    mSnapItemList = entity.getField(MultipleFields.SNAP_ITEMS);
                    MultiSnapRecyclerView multiSnapRecyclerView = holder.getView(R.id.multi_snap_recycler_view);
                    AppCompatTextView tvCountDownTextA = holder.getView(R.id.count_down_timer_text_a);
                    AppCompatTextView tvCountDownTextB = holder.getView(R.id.count_down_timer_text_b);
                    milliseconds = entity.getField(MultipleFields.SNAP_ITEMS_REMAIN_TIME);
                    mMainDownTimerView = holder.getView(R.id.count_down_timer_view);
                    if (milliseconds != 0) {
                        // 1 . initViews
                        mMainDownTimerView.setDownTime(milliseconds);
                        // 2 . initEvents
                        mMainDownTimerView.setDownTimerListener(new OnCountDownTimerListener() {

                            @Override
                            public void onFinish() {
                                Toast.makeText(mContext, "抢购结束", Toast.LENGTH_SHORT).show();
                            }
                        });
                        // 3 . StartDownTimer
                        mMainDownTimerView.startDownTimer();
                    } else {
                        mMainDownTimerView.setVisibility(View.INVISIBLE);
                        tvCountDownTextA.setVisibility(View.INVISIBLE);
                        tvCountDownTextB.setText("已结束");
                    }
                    LinearLayoutManager multiSnapHorizontalManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
                    MultiSnapHorizontalAdapter multiSnapHorizontalAdapter = new MultiSnapHorizontalAdapter(mSnapItemList);
                    multiSnapRecyclerView.setLayoutManager(multiSnapHorizontalManager);
                    multiSnapRecyclerView.setAdapter(multiSnapHorizontalAdapter);
                    mIsRushItem = true;
                }
                break;
            case ItemType.HEADER_MAIN_IMG:
                headerImgUrl = entity.getField(MultipleFields.HEADER_MAIN_IMG_URL);
                Glide.with(mContext).load(headerImgUrl).apply(RECYCLER_OPTIONS).into((AppCompatImageView) holder.getView(R.id.item_header_main_img));
                break;
            case ItemType.INDEX_GOODS:
                goodsTag = entity.getField(MultipleFields.GOODS_TAG);
                goodsName = entity.getField(MultipleFields.GOODS_NAME);
                goodsPrice = entity.getField(MultipleFields.GOODS_PRICE);
                goodsUnit = entity.getField(MultipleFields.GOODS_UNIT);
                imageUrl = entity.getField(MultipleFields.IMAGE_URL);
                Glide.with(mContext).load(imageUrl).apply(RECYCLER_OPTIONS).into((AppCompatImageView) holder.getView(R.id.index_goods_img));
                holder.setText(R.id.index_goods_tag, goodsTag);
                holder.setText(R.id.index_goods_name, goodsName);
                holder.setText(R.id.index_goods_price, goodsPrice);
                holder.setText(R.id.index_goods_unit, goodsUnit);
                break;
            default:
                break;
        }
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(MultipleFields.SPAN_SIZE);
    }

    @Override
    public void onItemClick(int position) {
        String desc = String.format("您点击了第%d项，内容是%s", position+1, mData.get(position));
        Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show();
    }

    public void refresh(List<MultipleItemEntity> data) {
        getData().clear();
        setNewData(data);
        notifyDataSetChanged();
    }

    public void clearAll() {
        getData().clear();
        notifyDataSetChanged();
    }

    public void setIndexDelegate(IndexDelegate indexDelegate) {
        this.DELEGATE = indexDelegate;
    }
}
